package com.server.game.transaction;

import com.msg.MsgCommon;
import com.msg.MsgGame;
import com.msg.MsgMain;
import com.server.net.message.MsgDispatcherManager;
import com.server.net.message.MsgPack;
import com.server.net.session.Session;
import com.server.net.session.SessionManager;
import com.server.orm.OrmTransaction;
import com.server.orm.db.table.TB_role;
import com.server.orm.db.table.TB_token;
import com.server.orm.db.table.TB_user;
import com.server.util.ConfPropertiesUtil;
import com.server.util.LogUtil;

import java.util.List;

/**
 * 登录注册逻辑
 */
public class TLogin extends OrmTransaction {

    private String channelLongText;
    private String token;
    private MsgPack msgPack;

    public TLogin(MsgPack msgPack, String token) {
        this.msgPack = msgPack;
        this.channelLongText = msgPack.getChannel().id().asLongText();
        this.token = token;
    }

    @Override
    public boolean action() {
        if (token.trim().isEmpty()) {
            return false;
        }
        //TODO 验证token
        if (!ConfPropertiesUtil.getInstance()._DEBUG) {
            LogUtil.game.error("正式环境还啥都没有呢" + token);
            return false;
        } else {
            LogUtil.game.debug("debug开发环境登录：" + token);
        }

        //验证token后存储对应关系
        MsgMain.MainProtocl.Builder responseMsg = MsgMain.MainProtocl.newBuilder();
        MsgGame.SCLogin.Builder scmsg = MsgGame.SCLogin.newBuilder();
        responseMsg.setMessageType(MsgMain.MessageType.SC_Login);

        long userId = 0;
        //1.session存储对应关系
        if (SessionManager.getInstance().userLogin(token, this.channelLongText)) {
            Session session = SessionManager.getInstance().getSessionByToken(token);
            if (null == session) {
                return false;
            }
            //2.处理线程分配
            MsgDispatcherManager.getInstance().onLine(token);
            scmsg.setResult(true);
            //3.如果没登录过，创建一个user与token关联上
            TB_token tb_token = TB_token.get(token);
            if (null == tb_token) {
                //创建新user
                TB_user tb_user = new TB_user();
                userId = TB_user.insert(tb_user);
                //建立关联
                tb_token = new TB_token();
                tb_token.getData().setUserId(userId);
                TB_token.insert(token, tb_token);
            } else {
                //读取角色列表
                userId = tb_token.getData().getUserId();
                TB_user tb_user = TB_user.get(userId);
                if (null == tb_user) {
                    //新号默认创建userId
                    scmsg.setResult(false);
                } else {
                    List<Long> roles = tb_user.getData().getRoleIds();
                    for (long roleId : roles) {
                        TB_role tb_role = TB_role.get(roleId);
                        TB_role.B_role b_role = tb_role.getData();
                        MsgCommon.RoleLoginInfo.Builder roleLoginInfo = MsgCommon.RoleLoginInfo.newBuilder();
                        roleLoginInfo.setRoleId(roleId);
                        roleLoginInfo.setName(b_role.getName());
                        roleLoginInfo.setConfIndex(1);
                        scmsg.addRoleList(roleLoginInfo);
                    }
                }
            }
            //4.设置token存入channel
            msgPack.setToken(token);
            //session存userId,token
            session.setUserId(userId);
            session.setToken(token);
        } else {
            scmsg.setResult(false);
        }
        responseMsg.setSCLogin(scmsg);
        SessionManager.getInstance().sendMsgByToken(this.token, responseMsg);
        return true;
    }
}
